<template>
  <div class="app-container">
    <el-row>
      <!-- 搜索区域 -->
      <el-card style="margin:0 0 5px 5px;" v-show="showSearch">
        <el-form ref="queryForm" :inline="true" :model="queryData">
          <el-form-item label="所属分类" prop="catalogPath">
            <el-cascader
              v-model="catalogPath"
              filterable
              clearable
              placeholder="试试搜索：手机"
              :options="categoryData"
              :props="categoryProps"
              @change="handleChangeCatalog"
            />
          </el-form-item>
          <el-form-item label="选择品牌" prop="Q_brandId_L_EQ">
            <el-select placeholder="请选择" v-model="queryData.Q_brandId_L_EQ" filterable clearable>
              <el-option
                v-for="item in brandData"
                :key="item.brandId"
                :label="item.name"
                :value="item.brandId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="Q_publishStatus_S_EQ">
            <el-select v-model="queryData.Q_publishStatus_S_EQ" placeholder="状态" clearable>
              <el-option label="全部" value=""/>
              <el-option
                v-for="item in optionOfPublishStatus"
                :key="item.dictCode"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item class="fr p-r-10">
            <el-button class="filter-item" type="primary" round icon="el-icon-search" @click="handleFilter">
              搜索
            </el-button>
            <el-button class="filter-item m-l-10" type="info" round icon="el-icon-refresh" @click="handleClear">
              清空
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <!-- table区域 -->
      <el-card class="m-l-5">
        <el-row :gutter="10" class="m-b-5">
          <table-toolbar :showSearch.sync="showSearch" @refresh="getTableData" />
        </el-row>
        <el-table
          ref="spu_table"
          v-loading="tableLoading"
          row-key="id"
          :data="tableData"
          element-loading-text="加载中"
          border
          fit
          highlight-current-row
          show-overflow-tooltip
          height="100px"
          v-el-height-adaptive-table="{bottomOffset: 110}"
          @sort-change="sortChange"
        >
          <el-table-column align="center" label="名称" prop="spuName" width="260" show-overflow-tooltip />
          <el-table-column align="center" label="描述" prop="spuDescription" show-overflow-tooltip />
          <el-table-column align="center" label="分类" width="120">
            <template slot-scope="scope">
              <el-tag type="success" v-text="scope.row.catalogName" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="品牌" prop="brandId" sortable width="120">
            <template slot-scope="scope">
              <el-tag type="primary" v-text="scope.row.brandName" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="上架状态" prop="publishStatus" sortable width="120">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.publishStatus !== null" :type="getDictType('status', scope.row.publishStatus)" v-text="getDictValue('status', scope.row.publishStatus)" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="创建时间" prop="createTime" sortable width="150" show-overflow-tooltip />
          <el-table-column align="center" label="修改时间" prop="updateTime" sortable width="150" show-overflow-tooltip />
          <el-table-column align="center" label="操作" width="160" fixed="right">
            <template slot-scope="scope">
              <el-link v-if="scope.row.publishStatus !== '0'" class="m-r-10" type="primary" icon="el-icon-plus" @click="handlePublish(scope.row.id)">上架</el-link>
              <el-link v-if="scope.row.publishStatus === '0'" class="m-r-10" type="danger" icon="el-icon-minus" @click="handleCancel(scope.row.id)">下架</el-link>
              <el-dropdown>
                  <span class="el-dropdown-link">
                    更多<i class="el-icon-arrow-down el-icon--right" />
                  </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item>
                    <el-link type="primary" icon="el-icon-view" @click="handlePreview(scope.row)">预览</el-link>
                  </el-dropdown-item>
                  <el-dropdown-item>
                    <el-link type="warning" icon="el-icon-plus" @click="handleBaseAttr(scope.row)">规格参数</el-link>
                  </el-dropdown-item>
                  <el-dropdown-item v-if="scope.row.publishStatus !== '0'">
                    <el-link type="danger" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-link>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="tableQuery.total>0" :total="tableQuery.total" :page.sync="tableQuery.page" :limit.sync="tableQuery.limit" @pagination="getTableData"/>
      </el-card>
    </el-row>

    <el-dialog v-el-drag-dialog title="商品详情" :visible.sync="openDetailDialog" fullscreen :append-to-body="true">
      <spu-detail ref="spu_detail_dialog" />
    </el-dialog>

    <base-attr-dialog v-if="openFormDialog" ref="base_attr_dialog" />
  </div>
</template>

<script>
import { brandList } from '@/api/mall/product/brand'
import { categoryTreeData } from '@/api/mall/product/category'
import { spuTable, spuPublish, spuCancel, spuDelete } from '@/api/mall/product/spu'
import SpuDetail from './form'
import BaseAttrDialog from './attr/baseDialog'

export default {
  components: {
    SpuDetail,
    BaseAttrDialog
  },
  data() {
    return {
      brandData: [],
      categoryProps: {
        value: 'id',
        label: 'name'
      },
      categoryData: [],
      catalogPath: [],
      optionOfPublishStatus: [],
      showSearch: true,
      queryData: {
        Q_catalogId_L_EQ: undefined,
        Q_brandId_L_EQ: undefined,
        Q_publishStatus_S_EQ: undefined
      },
      tableData: [],
      tableLoading: false,
      tableQuery: {
        page: 1,
        limit: 20,
        total: 0,
        sort: undefined,
        order: undefined
      },
      openFormDialog: false,
      openDetailDialog: false
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    async init() {
      await this.getBrandData()
      await this.getCategoryData()
      await this.getDictData()
      this.getTableData()
    },
    getBrandData() {
      brandList().then(response => {
        this.brandData = response
      })
    },
    getCategoryData() {
      categoryTreeData({
        from: 'vue',
        excludeRoot: 'true'
      }).then(response => {
        this.categoryData = response
      }).catch(() => {})
    },
    handleChangeCatalog(value) {
      this.queryData.Q_catalogId_L_EQ = value[value.length - 1]
    },
    getDictData() {
      this.$dictGroup('sys_shelf_status').then(response => {
        this.optionOfPublishStatus = response
      }).catch(e => {})
    },
    getDictType(field, value) {
      return this.$api.getDictElement(this.optionOfPublishStatus, value)[0]
    },
    getDictValue(field, value) {
      return this.$api.getDictElement(this.optionOfPublishStatus, value)[1]
    },
    handleFilter() {
      this.tableQuery.page = 1
      this.getTableData()
    },
    handleClear() {
      this.$refs.queryForm.resetFields()
      this.getTableData()
    },
    getTableData() {
      const query = Object.assign(this.tableQuery, this.queryData)
      this.tableLoading = true
      spuTable(query).then(response => {
        this.tableData = response.param.data
        this.tableQuery.total = response.param.recordsTotal
        this.tableLoading = false
        this.$refs.spu_table.bodyWrapper.scrollTop = 0
      }).catch(e => {
        this.tableLoading = false
      })
    },
    sortChange(data) {
      const {prop, order} = data
      this.tableQuery.sort = prop
      if (order === 'ascending') {
        this.tableQuery.order = 'asc'
      } else {
        this.tableQuery.order = 'desc'
      }
      this.getTableData()
    },
    handlePublish(id) {
      this.$confirm('是否上架所选数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        spuPublish(id).then(response => {
          this.$modal.msgSuccess(response.msg)
          this.getTableData()
        }).catch(() => {})
      }).catch(e => {})
    },
    handleCancel(id) {
      this.$confirm('是否下架所选数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        spuCancel(id).then(response => {
          this.$modal.msgSuccess(response.msg)
          this.getTableData()
        }).catch(() => {})
      }).catch(e => {})
    },
    handleDelete(id) {
      this.$confirm('是否删除所选数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        spuDelete(id).then(response => {
          this.$modal.msgSuccess(response.msg)
          this.getTableData()
        }).catch(() => {})
      }).catch(e => {})
    },
    handlePreview(row) {
      this.openDetailDialog = true
      this.$nextTick(() => {
        this.$refs.spu_detail_dialog.loadForm(row.id)
      })
    },
    handleBaseAttr(row) {
      this.openFormDialog = true
      this.$nextTick(() => {
        this.$refs.base_attr_dialog.init(row.id, row.catalogId)
      })
    }
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}
</style>
